﻿<?php
/*
Plugin Name: z2glinksites
Description: Ajouter des liens externes
Version: 0.1
Author:Z2gtechnology
Author URI: http://z2gtechnology.com
 */
 
 add_action('add_meta_boxes','z2glinksite_metaboxes');
/*
 * Permet de gerer  les metabox
 */
function z2glinksite_metaboxes()
{
    //add_meta_box("z2glink", 'Lien','z2glink_metabox','z2glink', 'normal','high') ;
   $screens = array( 'post', 'page' );

    foreach ( $screens as $screen ) {
        add_meta_box(
            'z2glink_linkid',
          /*  __( 'Link to the french site', 'z2glink_linksite' ),*/
		  'Link to the french site',
            'z2glink_metabox',
            $screen
        );
    }
}

/**
 * Metabox  pour gere le lien
 */
function z2glink_metabox($object)
{
    // ajout d'un token pour la securité 
    wp_nonce_field('z2glink','z2glink_nonce');
?>

<div class="meta-box-item-title">
    <!--h4>Link</h4-->
</div>
<div class="meta-box-item-content">
    <input type="text" name="z2glink_link"  style="width:100%" value="<?=  esc_attr(get_post_meta($object->ID,'_z2glink',true));?>">
</div>
<?php
    
}

add_action('save_post','z2glink_save_post',10,2);
add_action('save_page','z2glink_save_post',10,2);

function z2glink_save_post($post_id,$post)
{
   
   // verificationde la presencedela variable dans le post et de  la securité  pour eviter  les injection par xss
    if(!isset($_POST['z2glink_link'])|| !wp_verify_nonce($_POST['z2glink_nonce'],'z2glink'))
    {
         return $post_id;
    }
    $type =  get_post_type_object($post->post_type);
    if(!current_user_can($type->cap->edit_post))
    {
       return $post_id; 
    }
        update_post_meta($post_id, '_z2glink',$_POST['z2glink_link']);
}

// including teh  widget class
//ini_set('allow_url_include',1);
//require_once (plugins_url().'/z2glinksites/classes/Z2glink_widget.php');
require_once ('classes/Z2glink_widget.php');

//  init    
add_action( 'widgets_init', 'register_z2glink_widget' ); 

//  function to register the widget
function register_z2glink_widget() 
{  
    register_widget( 'Z2glink_widget' );  
}  


/**
 * Section  options panel
 */
 require_once'z2goptionspanel.php';
?>